package org.example.game.history;

import java.util.Arrays;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/07/06 22:33
 */
public class Solution100336 {

    public int numberOfAlternatingGroups(int[] colors) {
        int k = 3;
        boolean[] dp = new boolean[colors.length];
        Arrays.fill(dp, false);
        boolean gap = k % 2 == 0;
        int sum = 0;
        for (int i = 0; i < colors.length; i++) {
            boolean last = false;
            if(i > 0){
                last = dp[i-1];
            }
            if(last){
                if(gap && colors[i] != colors[(i + k - 1)%colors.length]){
                    dp[i] = true;
                }else if(!gap && colors[i] == colors[(i + k - 1)%colors.length]){
                    dp[i] = true;
                }else {
                    dp[i] = false;
                }
            }else {
                dp[i] = true;
                for (int j = i; j < i + k - 1; j++) {
                    if(colors[j%colors.length] == colors[(j+1)%colors.length]){
                        dp[i] = false;
                        break;
                    }
                }
            }

            if(dp[i]){
                sum++;
            }
        }
        return sum;
    }

}
